package com.bolt.admin.module.sys.service;


import com.bolt.admin.module.sys.entity.PrivilegeEntity;
import com.bolt.admin.module.sys.repository.PrivilegeRepository;
import com.bolt.support.base.service.BaseService;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional(readOnly = true,rollbackFor = Exception.class)
public class PrivilegeService extends BaseService<PrivilegeEntity, String> {


    @Autowired
    private PrivilegeRepository privilegeRepository;


    @Override
    protected BasicJpaRepository<PrivilegeEntity, String> getRepository() {
        return privilegeRepository;
    }

    public List <PrivilegeEntity> findAllCached() {
        return privilegeRepository.findAllCached();
    }


    public List <PrivilegeEntity> findMenuButtonPrivilege(Long menuId) {
        return privilegeRepository.findPrivileges(menuId, PrivilegeEntity.PrivilegeTypeEnum.BUTTON);
    }

    public List <PrivilegeEntity> findButtonPrivileges(String roleId) {
        return privilegeRepository.findButtonPrivileges(roleId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateRelatedRoleR2s(String id, String[] roleIds) {
        PrivilegeEntity entity = privilegeRepository.getOne(id);
        updateRelatedR2s(entity, roleIds, "roleR2Privileges", "role");
    }

    public List <String> findIdsBymenuIds(Long[] menuIds) {
        return privilegeRepository.findPrivilegeIdsByMenuId(menuIds);
    }

    public List <String> findIdsBymenuIds(Long[] menuIds, PrivilegeEntity.PrivilegeTypeEnum type) {
        return privilegeRepository.findPrivilegeIdsByMenuId(menuIds, type);
    }

    public List <PrivilegeEntity> findPrivileges(Set<String> roleCodes) {
        return privilegeRepository.findPrivileges(roleCodes);
    }

}
